from flask import Blueprint, request, jsonify, send_file
import services.sqlService as sqlService

sql_bp = Blueprint("sql",__name__)


@sql_bp.route('/formatSql', methods=['POST'])
def format_sql():
    try:
        data = request.get_json()
        if not data or 'sql' not in data or 'action' not in data:
            return jsonify({'error': 'Invalid request'}), 400

        sql = data['sql']
        action = data['action']

        # 基本验证：SQL 不能为空
        if not sql.strip():
            return jsonify({'error': 'SQL cannot be empty'}), 400

        return sqlService.sqlFormat(sql, action)
    except Exception as e:
        return jsonify({'error': str(e)}), 500